package pers.vinsonws.jianghu.clientwin.ui;

import pers.vinsonws.jianghu.clientwin.global.Info;
import pers.vinsonws.jianghu.clientwin.service.ApplyChatRoomService;
import pers.vinsonws.jianghu.clientwin.service.SendAndRecvMsgService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * @auther vinsonws
 * @date 2018/11/12 22:12
 */
public class ChatOneUi extends JFrame {
    private static final long serialVersionUID = 5L;
    private JPanel mainPanel;                /*主面板*/
    private JTextArea chatArea;
    private JTextArea editArea;
    private JTextArea infoArea;
    private JButton sendMsg;

    public ChatOneUi() throws HeadlessException {
        mainPanel = new JPanel();
        mainPanel.setOpaque(false);
        mainPanel.setLayout(null);
//        mainPanel.setBackground(Color.lightGray);
//        setBackground(Color.lightGray);
        //setContentPane(mainPanel);

        chatArea = new JTextArea();
        chatArea.setLineWrap(true);
        chatArea.setFont(new Font("宋体", Font.BOLD, 16));
        chatArea.setWrapStyleWord(true);
        chatArea.setEditable(false);
        JScrollPane jscrollPaneChat = new JScrollPane(chatArea);
        jscrollPaneChat.setBounds(13, 10, 900, 340);
        jscrollPaneChat.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        mainPanel.add(jscrollPaneChat);

        editArea = new JTextArea();
        editArea.setLineWrap(true);
        editArea.setFont(new Font("宋体", Font.BOLD, 16));
        editArea.setWrapStyleWord(true);
        JScrollPane jscrollPane = new JScrollPane(editArea);
        jscrollPane.setBounds(13, 360, 900, 200);
        jscrollPane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        //屏蔽回车换行
        editArea.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "none");

        mainPanel.add(jscrollPane);

        sendMsg = new JButton("发送消息(Enter)");
        sendMsg.setFont(new Font("宋体", Font.PLAIN, 12));
        sendMsg.setBounds(750, 560, 150, 30);
        sendMsg.setContentAreaFilled(false);
        mainPanel.add(sendMsg);




        infoArea = new JTextArea();
        infoArea.setLineWrap(true);
        infoArea.setFont(new Font("宋体", Font.BOLD, 16));
        infoArea.setWrapStyleWord(true);
        infoArea.setEditable(false);
        JScrollPane jscrollPaneInfo = new JScrollPane(infoArea);
        jscrollPaneInfo.setBounds(920, 0, 200, 550);
        jscrollPaneInfo.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);



        mainPanel.add(jscrollPaneInfo);


    }

    /**
     * 用户打开一个用户
     * @param userId
     * @param name
     * @return
     */
    public JPanel openChatOneWin(Integer userId, String name){
        infoArea.setText("\n用户编号:" + userId + "\n\n用户名称:" + name + "\n\n\n用简介：\n\n\n\n\n\n" + "暂无"
                +"\n\n\n" + "我的名称:" + Info.userName);

        //关闭按钮
        JButton btn = new JButton("X");
        JLabel lab = new JLabel(name);
        JTabbedPane pane = Info.indexUi.getTabbedPane();
        JPanel jp=new JPanel();
//        GridLayout gl= new GridLayout(1,1,10,0);
//        jp.setLayout(gl);
        lab.setHorizontalAlignment(JLabel.LEFT);//设置文字显示在最左边

        btn.setHorizontalAlignment(JLabel.RIGHT);// 设置文字显示在最右
        lab.setOpaque(false);
        //btn.setBounds(0, 0, 10, 10);
        btn.setContentAreaFilled(false);
        btn.setFont(new Font("宋体", Font.PLAIN, 12));
        btn.setMargin(new Insets(0, 0, 0, 0));
        btn.setBorder(null);
        jp.add(lab);
        jp.add(btn);
        pane.addTab(name,mainPanel);
        pane.setTabComponentAt(pane.indexOfComponent(mainPanel),jp);

        //Info.indexUi.getTabbedPane().addTab(name, mainPanel);
        btn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                pane.remove(mainPanel);
                Info.userOpened.remove(userId);
                dispose();
            }
        });

        //发送按钮注册
        sendMsg.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String chat = "我" + ":" + editArea.getText() + "\n";
                chatArea.append(chat);
                SendAndRecvMsgService.sendChatOneMsg(editArea.getText(), userId);
                editArea.setText("");
            }
        });

        editArea.addKeyListener(new KeyAdapter()//键盘监听按钮
        {
            public void keyPressed(KeyEvent e)
            {
                if(e.getKeyCode()==KeyEvent.VK_ENTER){

                    String chat = "我" + ":" + editArea.getText() + "\n";
                    chatArea.append(chat);
                    SendAndRecvMsgService.sendChatOneMsg(editArea.getText(), userId);
                    editArea.setText("");
                }

            }

        });

        return mainPanel;
    }

    public void addChatText(String sendUserName, long sendTime, String text){
        String chat = sendUserName + ":" + text + "\n";
        chatArea.append(chat);
    }
}
